﻿#include <stdio.h>
#include <graphics.h>
#include <windows.h>	//创建随机数用
#include <math.h>

#define	WindWidth 640
#define WindHeight 480
#define BerryAmount 6

IMAGE img;

struct Berry {
	int x;
	int y;
	int r;
}berry[BerryAmount];		//可以直接这样instanciate结构体,甚至可以数组[]的形式instanciate一堆

struct Board {
	int x;
	int y;
	int score;

}board;

//搞个全局变量吧,问题应该不大
char arr[20] = "";

void GameInit() {

	//init the berry
	for (int i = 0; i < BerryAmount; i++) {
		berry[i].x = rand() % WindWidth;
		berry[i].y = rand() % (WindHeight/4);
		berry[i].r = 30;
	}


	//init the board
	board.x = WindWidth / 2 - 50;
	board.y = WindHeight - 20;
	board.score = 0;

	//加载图像
	loadimage(&img,"./berry_src.jpg",40,40);

}

bool DistJudge(Berry* bry ,int collx,int colly) {	//圆心到滑块左右上角的距离是否小于半径
	return sqrt((abs(bry->x-collx))^2+ (abs(bry->y - colly)) ^ 2)<= bry->r;
}


void ResetBerryPos(Berry* bry) {
	bry->y = 0;
	bry->x = rand() % WindWidth;
}

void CollisionJudge(Berry* bry) {
	if ((bry->x >= board.x) && (bry->x <= board.x + 100)) {
		//上平面
		board.score += 1;
		ResetBerryPos(bry);
	}
	else if ((bry->x >= board.x + 100) && (bry->x <= board.x + 100 + bry->r)){
		//右上点
		if (DistJudge(bry,board.x + 100, board.y)) {
			board.score += 1;
			ResetBerryPos(bry);
		}
	}
	else if ((bry->x >= board.x - bry->r) && (bry->x <= board.x )) {
		//左上点
		if (DistJudge(bry,board.x, board.y)) {
			board.score += 1;
			ResetBerryPos(bry);
		}
	}
	else {	//未触碰，继续下落
		return;
	}
}

void BerryDrift_Hor(Berry* bry) {
	if (rand() % 2) {	//任何数模2非0即1;
		if (bry->x + bry->r <= WindWidth) {
			bry->x += 2;
		}
		else {
			bry->x -= 2;
		}
	}
	else {
		if (bry->x - bry->r >= 0) {
			bry->x -= 2;
		}
		else {
			bry->x += 2;
		}
	}
}

void GameTick() {

	//接受键盘指令左右移动滑板
	if (GetAsyncKeyState(VK_LEFT)) {
		if (board.x - 10 >= 0) {
			board.x -= 10;
		}
	}
	if (GetAsyncKeyState(VK_RIGHT)) {
		if (board.x + 110 <= WindWidth) {
			board.x += 10;
		}
	}

	//球随机左右飘
	for (int i = 0; i < BerryAmount; i++) {
		BerryDrift_Hor(&berry[i]);
	}
	
	//球下落
	for (int i = 0; i < BerryAmount; i++) {
		berry[i].y += 1;
	}

	//是否触发碰撞判定
	for (int i = 0; i < BerryAmount; i++) {
		if ((berry[i].y >= board.y - berry[i].r) && berry[i].y <= WindHeight) {	//进入y周范围可判定
			CollisionJudge(&berry[i]);
		}
		else if (berry[i].y + berry[i].r >= WindHeight) {	//未触碰继续下落,触底重置位置
			board.score -= 1;
			ResetBerryPos(&berry[i]);
		}
	}
	
}

void GameDraw() {
	cleardevice();
	//setfillcolor(BLUE);
	//fillcircle(berry.x, berry.y, berry.r);	//三参定一圆

	for (int i = 0; i < BerryAmount; i++) {
		putimage(berry[i].x, berry[i].y, &img, SRCAND);
	}
		
	setfillcolor(LIGHTMAGENTA);
	fillrectangle(board.x, board.y, board.x+100, board.y+20);	//两点定一矩

	//board上显示分数
	setbkmode(TRANSPARENT);
	sprintf_s(arr, "分数:%d", board.score);
	outtextxy(board.x+5,board.y, arr);
	
}

int main()
{
	initgraph(WindWidth, WindHeight);
	setbkcolor(RGB(123,221,138));
	cleardevice();	//	清屏

	srand(GetTickCount());	//给随机数做种

	GameInit();

	while (1) {

		GameTick();
		GameDraw();
		Sleep(10);
	}
	return 0;

}

//
//总结：
//1、此番折腾，感觉指针的作用在于“特指性”，不用指针，函数拷贝一份对象，接着对拷贝对象放炮，放完就删掉，留下目标对象一脸蒙蔽
//2、